package org.handwerkszeug.tournament.impl;

import java.util.Comparator;

import org.handwerkszeug.tournament.Player;

public class SimpleOrder implements Comparator<Player> {

	@Override
	public int compare(Player left, Player right) {
		int result = 0;
		int lp = left.matchpoint();
		int rp = left.matchpoint();
		if (lp == rp) {
			result = opponentMatchPoint(left) - opponentMatchPoint(right);
		} else {
			result = lp - rp;
		}
		return result;
	}

	protected int opponentMatchPoint(Player p) {
		int result = 0;
		for (Player o : p.played()) {
			result += o.matchpoint();
		}
		return result;
	}
}
